<?php

namespace app\controller\admin\sky;

use app\model\admin\sky\SkyMagicData;
use app\model\admin\sky\SkyMagicDetail;
use app\model\admin\sky\SkyTaskData;
use laytp\controller\Backend;
use laytp\library\CommonFun;
use think\facade\Db;
use think\response\Json;

/**
 * Class Candle
 * @package app\controller\admin\sky
 */
class TaskData extends Backend
{
    /**
     *
     * @var Candle
     */
    protected $model;
    protected $hasSoftDel = 0;//是否拥有软删除功能

    protected $noNeedLogin = []; // 无需登录即可请求的方法
    protected $noNeedAuth = ['index', 'info']; // 无需鉴权即可请求的方法

    public $orderRule = ['id' => 'desc'];

    public function _initialize()
    {
        $this->model = new SkyTaskData();
    }

    /**
     * 列表
     *  all_data参数的值为true时，表示查询表中所有数据集，否则进行分页查询
     * @return mixed
     */
    public function index()
    {
        $where   = $this->buildSearchParams();
        $order   = $this->buildOrder();
        $allData = $this->request->param('all_data');
        if ($allData) {
            $data = $this->model->where($where)->order($order)->cache(true)->select()->toArray();
            foreach ($data as &$item){
                $item['task_img'] = json_decode($item['task_img'],true);
            }
        } else {
            $limit = $this->request->param('limit', 10);
            $data  = $this->model->where($where)->order($order)->paginate($limit)->toArray();
            foreach ($data['data'] as &$item){
               $item['task_img'] = json_decode($item['task_img'],true);
            }
        }
        return $this->success('数据获取成功', $data);
    }

    //添加
    public function add()
    {
        try {
            $post = $this->request->post();
            $saveData = [
                'icon' => $post['icon'],
                'task_name' => $post['task_name'],
                'task_desc' => $post['task_desc'],
                'target' => $post['target'],
                'target_season' => $post['target_season'],
                'task_img' => json_encode(explode(',', $post['image']), JSON_UNESCAPED_UNICODE),
            ];

            $saveAll = $this->model->save($saveData);
            if (!$saveAll) throw new \Exception('保存失败');

            $this->clearCache();
            return $this->success('操作成功');
        } catch (\Exception $e) {
            return $this->exceptionError($e);
        }
    }


    /**
     * @NodeAnotation(title="编辑")
     */
    public function edit()
    {
        $id = $this->request->param('id');
        $post = CommonFun::filterPostData($this->request->post());
        $row = $this->model->find($id);
        $row->icon = $post['icon'];
        $row->task_name = $post['task_name'];
        $row->task_desc = $post['task_desc'];
        $row->target = $post['target'];
        $row->target_season = $post['target_season'];
        $row->task_img = json_encode(explode(',', $post['image']), JSON_UNESCAPED_UNICODE);
        try {
            $save = $row->save();
            if ($save === false) throw new \Exception('保存失败');
            $this->clearCache();
            return $this->success('保存成功');
        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }


    public function info()
    {
        $id = $this->request->param('id');

        $task = $this->model->where('id', $id)->find()->toArray();
        $task['task_img'] = json_decode($task['task_img'], true);

        return $this->success('获取成功', $task);
    }
}